package com.isoft.c2team3service2.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.isoft.c2team3service2.entity.PrivatePost;
import com.isoft.c2team3service2.mapper.PrivatePostMapper;
import com.isoft.c2team3service2.service.ArtistService;
import com.isoft.c2team3service2.service.PrivatePostService;
import com.isoft.c2team3service2.service.SensitiveWordService;
import com.isoft.c2team3service2.util.UserHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
@Service
public class PrivatePostServiceImpl extends ServiceImpl<PrivatePostMapper,PrivatePost> implements PrivatePostService {
    @Autowired
    private ArtistService artistService;
    @Autowired
    private SensitiveWordService sensitiveWordService;
    @Autowired
    private PrivatePostMapper privatePostMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean sendPrivatePost(Integer artistId, String content) {
        Integer userId = UserHolder.getUser().getId();

        // 验证用户是否关注了该艺人
        if (!artistService.isFollowing(artistId)) {
            throw new RuntimeException("只有关注该艺人的用户才能发送私密帖子");
        }

        PrivatePost privatePost = new PrivatePost();
        privatePost.setUserId(userId);
        privatePost.setArtistId(artistId);
        privatePost.setContent(content);
        privatePost.setStatus(1);
        privatePost.setCreateTime(LocalDateTime.now());
        privatePost.setUpdateTime(LocalDateTime.now());

        String filteredContent = sensitiveWordService.filterText(content);
        if (filteredContent.endsWith("（敏感词）")) {
            return false;
        }else {
            privatePost.setFilteredContent(filteredContent);
        }
        return save(privatePost);
    }

    @Override
    public List<PrivatePost> getPrivatePosts(Integer artistId) {
        Integer userId = UserHolder.getUser().getId();
        if (artistId == null) {
            return List.of();
        }
        return List.of();
    }

    @Override
    public List<PrivatePost> getPrivateConversation(Integer artistId) {
        Integer userId = UserHolder.getUser().getId();

        // 只能查看自己与艺人的对话
        return privatePostMapper.selectPrivatePosts(userId, artistId);
    }


}
